package conf

/*******************************************************************************
Copyright:cloud
Author:cloudapex@126.com
Version:1.0
Date:2020-06-12
Description: 主配置文件
*******************************************************************************/
import (
	"encoding/json"
	"io/ioutil"

	"sync"

	"gitee.com/cloudapex/ulern/cto"
	"gitee.com/cloudapex/ulern/util"
)

type CommonSetting struct {
	RunMode string `json:"runMode"`

	ServerId   string
	ListenAddr string `json:"listenAddr"`

	SvrAuthKey string
	SvrSignKey string

	FilePath string `json:"filePath"`

	AdminIds []int64 `json:"adminIds"`

	UseTLS  bool
	CrtFile string
	KeyFile string
}
type ReportSetting struct {
	Enable bool   `json:"enable"`
	Env    string `json:"env"`
	ToAddr string `json:"toAddr"`
}

//------------------------------------------------------------------------------
//=====================================Main=====================================
//------------------------------------------------------------------------------
var Main *mainConf

func init() { Main = cto.Preload("MT_Main", &mainConf{}).(*mainConf) }

type mainConf struct {
	mutx sync.Mutex
	main *mainMain
}
type mainMain struct {
	Common CommonSetting
	Report ReportSetting `json:"report"`
}

func (this *mainConf) HandleInit() error {
	this.main = &mainMain{
		Common: CommonSetting{
			RunMode:    "debug", // or release
			ListenAddr: ":8090",
		}}
	data, err := ioutil.ReadFile(this.HandlePath())
	if err != nil {
		return err
	}
	return json.Unmarshal(data, this.main)
}
func (this *mainConf) HandlePath() string { return util.ExePathJoin("confs/permissions.json") }

func (this *mainConf) HandleContent() string {
	b, err := json.MarshalIndent(this.main, "", "\t")
	if err != nil {
		return err.Error()
	}
	return string(b)
}
func (this *mainConf) HandleReload() error {
	data, err := ioutil.ReadFile(this.HandlePath())
	if err != nil {
		return err
	}

	temp := &mainMain{Common: CommonSetting{}}
	if err := json.Unmarshal(data, temp); err != nil {
		return err
	}
	defer util.UnLock(util.Lock(&this.mutx))
	// update some fields
	return cto.ErrSettingNotNeed
}

//------------------------------------------------------------------------------
//================================开放方法=======================================
//------------------------------------------------------------------------------
func (this *mainConf) Stg() *mainMain {
	defer util.UnLock(util.Lock(&this.mutx))
	return this.main
}
func (this *mainConf) RunMode() string  { return this.Stg().Common.RunMode }
func (this *mainConf) ServerId() string { return this.Stg().Common.ServerId }

func (this *mainConf) ListenAddr() string { return this.Stg().Common.ListenAddr }
func (this *mainConf) SvrAuthKey() string { return this.Stg().Common.SvrAuthKey }
func (this *mainConf) SvrSignKey() []byte { return []byte(this.Stg().Common.SvrSignKey) }

func (this *mainConf) FilePath() string  { return this.Stg().Common.FilePath }
func (this *mainConf) AdminIds() []int64 { return this.Stg().Common.AdminIds }

func (this *mainConf) UseTLS() bool       { return this.Stg().Common.UseTLS }
func (this *mainConf) TLSCrtFile() string { return this.Stg().Common.CrtFile }
func (this *mainConf) TLSKeyFile() string { return this.Stg().Common.KeyFile }

func (this *mainConf) Report() ReportSetting { return this.Stg().Report }
